﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using DataAccess;

public partial class Step3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Exam"] == null )
            Response.Redirect("Default.aspx");

        if (Session["Step3"] != null)
        {
            Response.Redirect("Result.aspx");
        }


       if (!Page.IsPostBack)
       {
           //GET QUESTION BY CATEGORY COMPUTER
           SqlDataAdapter sdaQuestionComputer = new SqlDataAdapter("select Question.Content, Question.ID from ExamQuestion left join Question on ExamQuestion.QuestionID = Question.ID where Question.CategoryID = 3 and ExamQuestion.ExamID = " + Session["Exam"], sConfiguration.Configuration.Connection);
           DataTable tableQuestionComputer = new DataTable();
           sdaQuestionComputer.Fill(tableQuestionComputer);

           ListViewComputer.DataSource = tableQuestionComputer;
           ListViewComputer.DataBind();

       }
    }


    protected void ListItemDataBoundComputer(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            //determine if the row type is an Item
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                DbCommand cmd = GenericData.CreateCommand();
                cmd.CommandText = "SELECT * FROM Answer WHERE QuestionID = " + row["ID"];
                DataTable table = GenericData.ExecuteReader(cmd);

                RadioButtonList tmp = (RadioButtonList)e.Item.FindControl("lstQuestionView");
                if (tmp != null)
                {
                    tmp.DataTextField = "Content";
                    tmp.DataValueField = "ID";
                    tmp.DataSource = table;
                    tmp.DataBind();
                }
            }
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        int Count = 0;
        for (int i = 0; i < ListViewComputer.Items.Count; i++)
        {
            if (ListViewComputer.Items[i].Controls.Count > 0)
            {

                ListViewDataItem dataItem = (ListViewDataItem)ListViewComputer.Items[i];
                HiddenField hfQuestion = (HiddenField)dataItem.FindControl("hfQuestionID");
                String QuestionID = hfQuestion.Value.ToString();
                for (int j = 0; j < ListViewComputer.Items[i].Controls.Count; j++)
                {
                    if (ListViewComputer.Items[i].Controls[j] is RadioButtonList)
                    {
                        if (((RadioButtonList)ListViewComputer.Items[i].Controls[j]).SelectedItem != null)
                        {
                            String CandidateAnswer = ((RadioButtonList)ListViewComputer.Items[i].Controls[j]).SelectedItem.Value.ToString();
                            ViewState.Add(QuestionID, CandidateAnswer);
                            Count++;
                        }
                    }
                }
            }
        }


        if (Count < 5)
            Response.Write("<script>alert('You do not answer all the questions?');</script>");
        else
        {
            DataTable tableResult;
            int CountAnswerTrue = 0;
            //INSERT RESULT STEP1 IN TABLE CANDIDATEEXAMQUESITON
            DbCommand cmd = GenericData.CreateCommand();
            foreach (String item in ViewState.Keys)
            {
                cmd.CommandText = "INSERT INTO CandidateQuestionAnswer VALUES( " + Session["CandidateID"].ToString() + ", (SELECT ID FROM ExamQuestion WHERE QuestionID = " + item + " AND ExamID = " + Session["Exam"] + "), " + ViewState[item].ToString() + " )";
                //Response.Write("INSERT INTO CandidateQuestionAnswer VALUES( " + Session["CandidateID"].ToString() + ", (SELECT ID FROM ExamQuestion WHERE QuestionID = " + item + " AND ExamID = " + Session["Exam"] + "), " + ViewState[item].ToString() + " )" +"<br/>");
                GenericData.ExecuteNoneQuery(cmd);

                //get answer true
                cmd.CommandText = "select IsTrue From Answer where QuestionID = " + item + " and ID = " + ViewState[item].ToString();
                //Response.Write("select IsTrue From Answer where QuestionID = " + item + " and ID = " + ViewState[item].ToString() + "<br/>");

                tableResult = GenericData.ExecuteReader(cmd);
                if (tableResult.Rows.Count > 0)
                {
                    if ((bool)tableResult.Rows[0]["IsTrue"] == true)
                    {
                        CountAnswerTrue++;
                    }
                }

            }
            Session["CountAnserTrue"] = (Int32.Parse(Session["CountAnserTrue"].ToString()) + CountAnswerTrue).ToString();
            Session.Add("Step3", "ok"); 
            Response.Redirect("Result.aspx");
        }
    }
}